package Logic;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Database.AuditLogDB;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class CommitteeSearch_BLL
 */

public class CommitteeSearch_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeSearch_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		HttpSession session = request.getSession();

		

			String staff = request.getParameter("staff");
			String committee = request.getParameter("committee");

			AuditLogDB.insertLog("search committee", (String) request
					.getSession().getAttribute("name"));
			if (staff.equals("blank") && committee.equals("blank")) {
				response.sendRedirect("committee_searchcomm.jsp?error=1");
			} else if (staff.equals("blank") && !committee.equals("blank")) {
				// search by committee
				request.setAttribute("id", committee);
				request.getRequestDispatcher("committee_searchresult.jsp")
						.forward(request, response);
			} else if (!staff.equals("blank") && committee.equals("blank")) {
				ArrayList<CommitteeRole> crList = Database.CommitteeRoleDB
						.getAllCommitteeRole();

				String id = "";
				for (int i = 0; i < crList.size(); i++) {
					if (crList.get(i).getStaffName().contains(staff)) {
						id = id.concat(crList.get(i).getCommittee_id() + ",");
					}
				}

				if (id.equals("")) {
					response.sendRedirect("committee_searchresult.jsp?error=1");
				} else {
					request.setAttribute("id", id);
					request.getRequestDispatcher("committee_searchresult.jsp")
							.forward(request, response);
				}
			} else {
				ArrayList<CommitteeRole> crList2 = Database.CommitteeRoleDB
						.getCommitteeRole(committee);

				String id2 = "";
				for (int i = 0; i < crList2.size(); i++) {
					if (crList2.get(i).getStaffName().contains(staff)) {
						id2 = id2
								.concat(crList2.get(i).getCommittee_id() + ",");
					}
				}

				if (id2.equals("")) {
					response.sendRedirect("committee_searchresult.jsp?error=1");
				} else {
					request.setAttribute("id", id2);
					request.getRequestDispatcher("committee_searchresult.jsp")
							.forward(request, response);
				}
			}
		}

	
}
